package dao;

import entity.Review;
import entity.User;
import utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class UserDao {

    public boolean insertUser(User user) {
        String sql = "INSERT INTO users (USER_NAME, PASSWORD, SEX, PEICE_NAME, PHONE, PROVINCE, DISTRICT, DETAIL_ADDRESS) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getSex());
            preparedStatement.setString(4, user.getPeiceName());
            preparedStatement.setString(5, user.getPhone());
            preparedStatement.setString(6, user.getProvince());
            preparedStatement.setString(7, user.getDistrict());
            preparedStatement.setString(8, user.getDetailAddress());
            int rowsInserted = preparedStatement.executeUpdate();
            return rowsInserted > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }


    //用户名已存在
    public boolean userExists(String username) {
        String sql = "SELECT COUNT(*) FROM users WHERE USER_NAME = ?";
        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt(1) > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    //检索用户信息
    public HashMap<String, String> getUserByUsername(String username) {
        String sql = "SELECT * FROM users WHERE USER_NAME=?";
        HashMap<String,String> row = new HashMap<>();
        try (Connection connection = JDBCUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                row.put("username", resultSet.getString("USER_NAME"));
                row.put("password", resultSet.getString("PASSWORD"));
                row.put("userId", resultSet.getString("USER_ID"));
                row.put("roleId", resultSet.getString("ROLE_ID"));
                row.put("sex", resultSet.getString("SEX"));
                row.put("peiceName", resultSet.getString("PEICE_NAME"));
                row.put("phone", resultSet.getString("PHONE"));
                row.put("province", resultSet.getString("PROVINCE"));
                row.put("district", resultSet.getString("DISTRICT"));
                row.put("detailAddress", resultSet.getString("DETAIL_ADDRESS"));


            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row; //用户名不存在
    }

    //ResultSet对象转化为User对象
    private User convertResultSetToUser(ResultSet resultSet) throws SQLException {
        User user = new User();
        user.setUserId(resultSet.getInt("userId"));
        user.setRoleId(resultSet.getInt("roleId"));
        user.setUserName(resultSet.getString("userName"));
        user.setPassword(resultSet.getString("password"));
        user.setSex(resultSet.getString("sex"));
        user.setPeiceName(resultSet.getString("peiceName"));
        user.setPhone(resultSet.getString("phone"));
        user.setProvince(resultSet.getString("province"));
        user.setDistrict(resultSet.getString("district"));
        user.setDetailAddress(resultSet.getString("detailAddress"));

        return user;
    }

    public String getUserRole(int userId) {
        String sql = "SELECT ROLE_NAME FROM users u JOIN roles r ON u.ROLE_ID = r.ROLE_ID WHERE u.USER_ID = ?";
        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getString("ROLE_NAME");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

